angular.module('furion.services.modal_manager', [])

.factory "ModalManager", ($rootScope, $ionicModal, $ionicBackdrop)->
  modals =
    signin: {modal: null, template: 'templates/modals/signin.html', pageID: '#signin-page', open_at: null, backdrop: true}
    signup: {modal: null, template: 'templates/modals/signup.html', pageID: '#signup-page', open_at: null, backdrop: true}
    get_pass: {modal: null, template: 'templates/modals/get-pass.html', pageID: '', open_at: null, backdrop: true}
    signup_agreement: {modal: null, template: 'templates/modals/signup-agreement.html', pageID: '', open_at: null, backdrop: true}
    payments: {modal: null, template: 'templates/modals/payments.html', pageID: '#payments-modal', open_at: null, backdrop: false}
    pass_pad: {modal: null, template: 'templates/modals/modal-password-pad.html', pageID: '#password-pad-modal', open_at: null, backdrop: false}
    sms_pad: {modal: null, template: 'templates/modals/modal-sms-pad.html', pageID: '#password-pad-modal', open_at: null, backdrop: false}
    image_modal: {modal: null, template: 'templates/modals/modal-image.html', pageID: '#image-modal', open_at: null, backdrop: true}
    qrcode: {modal: null, template: 'templates/modals/modal-qrcode.html', pageID: "#qrcode-modal", open_at: null, backdrop: true}
    signoutConfirm: {modal: null, template: 'templates/modals/modal-signoutConfirm.html', pageID: '#signoutConfirm-modal', open_at: null, backdrop: true}
    share_tip: {modal: null, template: 'templates/modals/modal-share-tip.html', pageID: '#share-tip-modal', open_at: null, backdrop: true}
    touchid: {modal: null, template: 'templates/modals/touchid-modal.html', pageID: '#touchid-modal', open_at: null, backdrop: true}
    pop_up:  {modal: null, template: 'templates/modals/modal-popup.html', pageID: '#popup-modal', open_at: null, backdrop: true}
    edit_modal:  {modal: null, template: 'templates/modals/modal-edit.html', pageID: '#edit-modal', open_at: null, backdrop: true}
  openModal = (modal_name, params)->
    modal_config = modals[modal_name]

    # to fix signin open again
    # modal_config.modal = null

    return unless modal_config?
    return if modal_config.modal?

    # to fix async issue
    modal_config.modal = {}

    $ionicModal.fromTemplateUrl(modal_config.template, {
      backdropClickToClose: false,
      animation: 'scale-in'
    }).then( (modal) ->
      modal_config.modal = modal
      modal_config.open_at = (new Date).getTime()
      modal_config.modal.scope.goBack = $rootScope.goBack

      modal_config.modal.show().then ()->
        ele = $(modal_config.pageID)
        cur_scope = angular.element(ele).scope()
        if cur_scope
          cur_scope.modal_params = params
          cur_scope.modal_shown() if cur_scope.modal_shown

          cur_scope.$emit '$modal.enter'
        return

      unless modal_config.backdrop
        $ionicBackdrop.retain()
        $('.modal-backdrop').removeClass('modal-backdrop')
    )

  closeModalByConfig = (modal_config)->
    return unless modal_config?

    ele = $(modal_config.pageID)
    cur_scope = angular.element(ele).scope()
    cur_scope.modal_params = null if cur_scope

    modal_config.modal.remove() if modal_config.modal
    modal_config.modal = null
    modal_config.open_at = null

    unless modal_config.backdrop
      for modal, config of modals
        continue unless config.open_at
        if (not last_open_modal?) || config.open_at >= last_open_modal.open_at
          last_open_modal = config
      return false if last_open_modal?
      $ionicBackdrop.release()

  closeAllModal = ()->
    for key, config of modals
      closeModalByConfig config
    $ionicBackdrop.release()

  $rootScope.$on "modals:open", (event, params)->
    openModal(params.modal_name, params.params)
    event.preventDefault()

  $rootScope.$on "modals:close", (event, params)->
    modal_config = modals[params.modal_name]
    closeModalByConfig modal_config
    event.preventDefault()

  $rootScope.$on '$destroy', ()->
    closeAllModal()

  $rootScope.$on "close-all-modals", (e)->
    closeAllModal()

  closeLastModal = ()->
    last_open_modal = null
    for modal, config of modals
      continue unless config.open_at
      if (not last_open_modal?) || config.open_at >= last_open_modal.open_at
        last_open_modal = config
    return false unless last_open_modal?
    closeModalByConfig last_open_modal
    console.debug 'close last modal'
    return true

  return {closeLastModal: closeLastModal}
